/*
    Copyright (C) 2021 Huawei Device Co., Ltd.
    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
 */

package com.danikula.videocache.interfacers;

import com.danikula.videocache.ProxyCacheException;

import java.io.IOException;

/**
 * 分节点缓存策略接口
 */
public abstract class IOneFileCache extends Cache {
    /**
     * 读取缓存的时候 缓存内容是否足够
     *
     * @param offset 读取起点
     * @param length 读取长度
     * @return 缓存内容是否足够
     * @throws IOException IO错误
     */
    public abstract boolean isCacheEnough(long offset, int length) throws IOException;

    /**
     * 单文件多节点读取缓存数据 (边请求边读取的时候 涉及到已缓存区域的合并)
     *
     * @param buffer 读取byte数组
     * @param offset 读取起点
     * @param length 读取长度
     * @return 读取的数据
     * @throws ProxyCacheException 错误信息
     * @throws IOException         错误信息
     */
    public abstract int find(byte[] buffer, long offset, int length) throws ProxyCacheException, IOException;

    /**
     * 单文件多节点写入缓存数据
     *
     * @param data   请求的数据
     * @param length 请求的数据长度
     * @param offset 缓存起点
     * @throws ProxyCacheException 错误信息
     * @throws IOException         错误信息
     */
    public abstract void insert(byte[] data, int length, long offset) throws ProxyCacheException, IOException;

    /**
     * 获取网络请求数据起始位置
     *
     * @return 数据起始位置
     * @throws ProxyCacheException 错误信息
     */
    public abstract long getDownLoadStart() throws ProxyCacheException;

    /**
     * 获取网络请求数据终止位置
     *
     * @return 数据终止位置
     */
    public abstract long getDownLoadEnd();

    /**
     * 获取缓存数据总大小
     *
     * @return 缓存数据总大小
     */
    public abstract long getTotalCacheLength();

    /**
     * 写入线程是否停止 临时文件的缓存数据的写入过程是否停止
     *
     * @param isStop 写入过程是否停止
     */
    public abstract void setStop(boolean isStop);
}
